package com.alex.controller;

import com.alex.entity.Alexhqline;
import com.alex.entity.ShsqWater;
import com.alex.service.NoticeService;
import com.alex.service.ShQfqWaterService;
import com.alex.utils.AlexUtil;
import com.alex.utils.PageData;
import com.alex.utils.ResultBody;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shsq")
@Api(tags = "射洪前锋渠灌区水情信息")
public class ShQfqWaterController {

    private static Logger logger = LoggerFactory.getLogger(ShQfqWaterController.class);

    @Autowired
    private ShQfqWaterService shQfqWaterService;


    @PostMapping("/getOnlines")
    @ApiOperation("总数/在线")
    public ResultBody getOnlines(){
        Map<String, Object> res = shQfqWaterService.getOnlines();
        return res !=null?ResultBody.success("查询成功", res) : ResultBody.error("查询失败");
    }

    @PostMapping("/getDevices")
    @ApiOperation("查询站点信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", required = true, value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", required = true, value = "每页条数")
    })
    public ResultBody getDevices(@RequestParam("pageSize") Integer pageSize,@RequestParam("currentPage") Integer currentPage){
        PageData<ShsqWater> pds = shQfqWaterService.getShsqWaters(AlexUtil.getTableName(new Date()),pageSize,currentPage);
        return pds.getRows().size() > 0?ResultBody.success("查询成功", pds.getRows()) : ResultBody.error("查询失败");
    }

    @PostMapping("/getShsqWaters")
    @ApiOperation("获取站点数据信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", required = true, value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", required = true, value = "每页条数")
    })
    public ResultBody getShsqWaters(@RequestParam("pageSize") Integer pageSize,@RequestParam("currentPage") Integer currentPage){
        PageData<ShsqWater> pds = shQfqWaterService.getShsqWaters(AlexUtil.getTableName(new Date()),pageSize,currentPage);
        return pds.getRows().size() >= 0?ResultBody.success("查询成功", pds.getRows()) : ResultBody.error("查询失败");
    }


    @PostMapping("/getNowData")
    @ApiOperation("实时数据")
    public ResultBody getNowData(){
        List<ShsqWater> messageVo = shQfqWaterService.getNowData();
        String con = "【四川恒宣】水雨情实况通报("+ AlexUtil.getdiytm2() +"至未来24小时): 截至目前["+ AlexUtil.formatDatealex(new Date()) + "]水雨情数据：";
        String c1 = "";
        if (messageVo != null && messageVo.size() > 0){
            for(int n= 0;n<messageVo.size();n++){
                if (n==messageVo.size()-1){
                    if ("0066668806".equals(messageVo.get(n).getStcd())){
                        if (messageVo.get(n).getZ() >= 770){
                            //有雨量
                            c1 = messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm" + " / 水位" + messageVo.get(n).getZ() + "m" + " / 流量" + messageVo.get(n).getQ() + "m³/s(770m流量)," + c1;
                            //无雨量
                            //c1 = messageVo.get(n).getStname() + "水位" + messageVo.get(n).getZ() + "m" + " / 流量" + getq(messageVo.get(n).getZ()) + "m³/s(770m流量)," + c1;
                        }else {
                            //有雨量
                            c1 = messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm" + " / 水位" + messageVo.get(n).getZ() + "m" + " / 流量" + messageVo.get(n).getQ() + "m³/s," + c1;
                            //无雨量
                            //c1 = messageVo.get(n).getStname() + "水位" + messageVo.get(n).getZ() + "m" + " / 流量" + getq(messageVo.get(n).getZ()) + "m³/s," + c1;
                        }
                    }else if ("0066668805".equals(messageVo.get(n).getStcd())){
                        if (messageVo.get(n).getZ() != null){
                            //有水位
                            c1 = messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm" + " / 水位" + gettwoq(messageVo.get(n).getZ()) + "m," + c1;
                        }else {
                            //无水位
                            c1 = messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm," + c1;
                        }
                    }else {
                        c1 += messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm.";
                    }
                }else {
                    if ("0066668806".equals(messageVo.get(n).getStcd())){
                        if (messageVo.get(n).getZ() >= 770){
                            //有雨量
                            c1 = messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm" + " / 水位" + messageVo.get(n).getZ() + "m" + " / 流量" + messageVo.get(n).getQ() + "m³/s(770m流量)," + c1;
                            //无雨量
                            //c1 = messageVo.get(n).getStname() + "水位" + messageVo.get(n).getZ() + "m" + " / 流量" + getq(messageVo.get(n).getZ()) + "m³/s(770m流量)," + c1;
                        }else {
                            //有雨量
                            c1 = messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm" + " / 水位" + messageVo.get(n).getZ() + "m" + " / 流量" + messageVo.get(n).getQ() + "m³/s," + c1;
                            //无雨量
                            //c1 = messageVo.get(n).getStname() + "水位" + messageVo.get(n).getZ() + "m" + " / 流量" + getq(messageVo.get(n).getZ()) + "m³/s," + c1;
                        }
                    }else if ("0066668805".equals(messageVo.get(n).getStcd())){
                        if (messageVo.get(n).getZ() != null){
                            //有水位
                            c1 = messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm" + " / 水位" + gettwoq(messageVo.get(n).getZ()) + "m," + c1;
                        }else {
                            //无水位
                            c1 = messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm," + c1;
                        }
                    }else {
                        c1 += messageVo.get(n).getStnm() + messageVo.get(n).getPj() + "mm,";
                    }
                }
            }
            con = con + c1.replaceAll("李家岩水情-","");
            return ResultBody.success("查询成功", con);
        }else {
            return ResultBody.error("查询失败");
        }
    }

    public Double gettwoq(Double q){
        BigDecimal bg = new BigDecimal(q);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

}
